<template>
    <div style="width:100%;height:100vh">
        <button @click="uploadavater">开始调试</button>
        {{logdata}}
    </div>
</template>
<script>
export default {
    data(){
        return{
            logdata:"",
        }
    },
    methods:{
        async uploadavater(){
            var _this = this,imageLength=9;
            var image={localIds:[],serverId:[]};　　//接收微信接口返回的参数
            var imageUrlList=[]　　//用户绑定页面上显示图片的变量
            var mediaIds=[] //接收上传图片返回的serverId（该字段就是调用获取临时素材接口的媒体ID）
            //1.选择图片
            //1237378768e7q8e7r8qwesafdasdfasdfaxss111
            //1237378768e7q8e7r8qwesafdasdfasdfaxss111
            // let access_token="32_jakwFUQ7GsGFpC8hRsZuPf-6vXa4FM_uxxGZUEtKrdOxktVQbloBPjx3IbxpEK1plUaZsIVUUj3OYpva7_EGDvDK0pPVYKF2FvD1vpPROvt-tdyTQedWKVji5OpWCoywIPgOqXhiIxHWR3LkCUUfAIAMEE"
            // let uid= JSON.parse(localStorage.getItem("user")).user_id
            // console.log("access_token",access_token)
            // _getmedia({
            //     access_token:access_token,
            //     media_id:"1237378768e7q8e7r8qwesafdasdfasdfaxss111",
            //     user_id:uid
            // }).then(res=>{
            //     console.log("Imageavater",res)
            // })

            let localhref="http://market.starrymedia.vip/systemwximg123"
            await _getjsapiticket(localhref).then(res=>{
                console.log("_getjsapiticket",res)
                if(res.errcode==1){
                    localStorage.setItem("WXjsapi_ticket",JSON.stringify(res.data))
                    wx.config({
                        debug: true, 
                        appId: res.data.appid, // 必填，公众号的唯一标识
                        timestamp: res.data.timestamp, // 必填，生成签名的时间戳
                        nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
                        signature: res.data.signature,// 必填，签名，见附录1
                        jsApiList: [
                            "uploadImage","chooseImage","chooseWXPay","downloadImage"
                        ]
                    });
                }
            })
            _this.logdata=""
            await wx.ready(()=>{
                wx.chooseImage({
                    count: 1, //上传图片的张数，最多上传9张
                    sizeType: ['original', 'compressed'],　　// 可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album', 'camera'],　　// 可以指定来源是相册还是相机，默认二者都有
                    success: function(res1) {
                        console.log("uploadavater",res1)
                        _this.logdata="uploadavater:::"+JSON.stringify(res1)+"###"
                        wx.uploadImage({
                            localId:res1.localIds[0],
                            success: function(res2) {
                                alert("uploadImageSuccess",JSON.stringify(res2))
                                _this.logdata+="uploadImageSuccess:::"+JSON.stringify(res2)+"###"
                                try{
                                    let access_token=JSON.parse(localStorage.getItem("WXjsapi_ticket")).access_token
                                    alert("access_token",access_token)
                                    _this.logdata+="access_token:::"+JSON.stringify(access_token)+"###"
                                    _getmedia({
                                        access_token:access_token,
                                        media_id:res2.serverId
                                    }).then(res=>{
                                        alert("Imageavater",JSON.stringify(res))
                                        _this.logdata+="Imageavater:::"+JSON.stringify(res)
                                    })
                                }catch(e){
                                    alert("E::",JSON.stringify(e))
                                }
                            },
                            fail: function(e2) {
                                console.log("uploadImageError",JSON.stringify(e2));
                            }
                        });
                    },
                    fail: function(e1) {
                        console.log("uploadavater error",e1)
                    }
                });
            })
        }
    }
}
</script>